<!doctype html>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript" src="./json2.js"></script>
<!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>-->
<link rel="stylesheet" type="text/css" href="./rels/jquery.ui.css" />
<link rel="stylesheet" type="text/css" href="./rels/style.css" />


<script type="text/javascript">
//<!--
var usrToken;
var classDur;
function init(data) {
  if(window.console) console.log(data);
  usrToken=JSON.parse(data);
  
  if(usrToken.statues==0) {
	window.location.href='http://acm.hust.edu.cn/JudgeOnline/loginpage.php';
  }
  else if(usrToken.statues==1) {
    $("input[name='ojid']").val(usrToken.ojid);
  }
  else if(usrToken.statues==2) {
    $("input[name='ojid']").val(usrToken.ojid);
    $("input[name='sid']").val(usrToken.sid);
    $("input[name='pid']").val(usrToken.pid);
    $("input[name='name']").val(usrToken.name);
    $("input[name='email']").val(usrToken.email);
    if(parseInt(usrToken.is_hust) == 1) $("select[name='is_hust']").val("Yes");
    else $("select[name='is_hust']").val("No");
  }
}
$(function(){
  $.ajax({url:"register.php?get",success:init});
  $("#form select").change(function(){
    var self =this;
    if($(self).val()=="No") $("#sid").removeClass("ui-state-error", classDur);
    else {
      if(!$("input[name='sid']").val().match(/^[Uu]20[01]\d{6}$/)) $("#sid").addClass("ui-state-error", classDur);
      else $("#sid").removeClass("ui-state-error", classDur);
    }
  });
  $("#form input").focusout(function(){
    var self=this;
    $(self).parent().removeClass("ui-state-highlight", classDur);
    if($("select[name='is_hust']").val()=="Yes" && $(self).attr("name")=="sid") {
      if(!$(self).val().match(/^[Uu]20[01]\d{6}$/)) {
        $(self).parent().addClass("ui-state-error", classDur);
        return;
      }
      else $(self).parent().removeClass("ui-state-error", classDur);
    } else if($(self).attr("name")=="email") {
      if(!$(self).val().match(/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/)) {
        $(self).parent().addClass("ui-state-error", classDur);
        return;
      }
      else $(self).parent().removeClass("ui-state-error", classDur);
    } else if($(self).attr("name")=="name") {
		if($(self).val().match(/^\s*$/)) {
			$(self).parent().addClass("ui-state-error", classDur);
			return;
		}
	}
    $.post("register.php?check", $("#form").serialize(), function(data) {
      if(window.console) console.log(data);
      var result=JSON.parse(data);
      if(result.e) {
        alert(result.e);
      } else {
        if(result[$(self).attr("name")]) $(self).parent().addClass("ui-state-error", classDur);
        else $(self).parent().removeClass("ui-state-error", classDur);
      }
    });
  })
  .focus(function(){
    if($(this).attr("readonly")) return;
    $(this).parent().addClass("ui-state-highlight", classDur);
  });
  $("#btns input").click(function(){
	if($(".ui-statue-error").length) {
		alert("There is some field(s) error, please correct them, and submit.");
		return;
	}
    $.post("register.php?submit", $("#form").serialize(), function(data){
		if(window.console) console.log(data);
      var result=JSON.parse(data);
      if(result.success) {
        alert("Success");
		window.location.href="http://acm.hust.edu.cn/thx/contest.php?cid=1079";
      } else {
        $("#form>div").removeClass("ui-state-error", classDur);
        for(var k in result) $("#"+k).addClass("ui-state-error", classDur);
      }
    });
  })
});
//-->
</script>
</head>
<body>
<div class="form ui-corner-all">
<div class="ui-widget-header ui-corner-top">
<h4>阿里巴巴华中地区网络预赛报名表</h4>
</div>
<div class="ui-widget-content">
<form id="form">
  <div id="ojid" class="field"><span class="field-label">已登录的Online Judge账号</span><input type="text" class="field-input" name="ojid" readonly="true"/><span class="field-src"><a class="link login" href="http://acm.hust.edu.cn/JudgeOnline/loginpage.php">登录</a> / <a class="link logout" href="http://acm.hust.edu.cn/JudgeOnline/logout.php">登出</a></span></div>
  <div id="name" class="field"><span class="field-label">姓名</span><input type="text" class="field-input" name="name"/><span class="field-src">必填</span></div>
  <div id="sid" class="field"><span class="field-label">学号</span><input type="text" class="field-input" name="sid"/><span class="field-src">请确保唯一性（仅检测华中科大学生）</span></div>
  <div id="pid" class="field"><span class="field-label">身份证号</span><input type="text" class="field-input" name="pid"/><span class="field-src">请填写您的正确的身份证号</span></div>
  <div id="email" class="field"><span class="field-label">Email</span><input type="text" class="field-input" name="email"/><span class="field-src">请确保唯一性</span></div>
  <div id="is_hust" class="field"><span class="field-label">是否是华中科技大学学生</span><select name="is_hust"><option>Yes</option><option>No	</option></select></div>
</form>
<hr/>
<div id="btns" class="ui-corner-bottom">
  <div>
  <input type="button" value="提交"/>
  </div>
</div>
</div>
</body>
</html>

